<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="./layui/css/layui.css">
    <style type="text/css">
        @media screen and (max-width: 768px) {
            .layui-table-tool{
                line-height: 22px;
                padding: 0.5rem 0.5rem;
            }
            .layui-inline .layui-form-label {
                font-size: 0.7rem!important;
                padding: 0!important;
                width: 40%!important;
            }
            .layui-input-inline .layui-input{
                height: 20px!important;
                font-size: 0.6rem!important;
                padding-left: 0!important;
            }
            .layui-input-inline{
                width: 6rem!important;
            }
            .layui-table-tool-temp .layui-btn{
                height: 1.5rem;
                line-height: 1rem;
                padding: 0 0.3rem;
                font-size: 0.6rem;
            }
            .layui-table-tool-temp{
                padding-right: 0!important;
                width: 5.3rem!important;
            }
            .layui-table-tool-self{
                right: 0.4rem!important;
            }
            .layui-table-tool .layui-inline[lay-event] {
                width: 1rem;
                height: 1rem;
                padding: 0;
            }
            .layui-icon {
                font-size: 0.9rem;
            }
            body{
                height: 30rem!important;
            }
            .layui-table-view{
                height:100%!important;
            }
            .layui-table-body{
                height:100%!important;
            }
            .layui-table-tool{
                padding: 0.6rem 0.4rem;
            }
            .layui-table-view .layui-table{
                width: 100%!important;
            }
            .layui-table-page{
                overflow: scroll;
            }
            .layui-table-column{
                padding: 0.5rem 0.2rem;
            }
            .layui-table-page .layui-laypage a{
                padding: 0 0.4rem;
                font-size: 0.6rem;
            }
            .layui-table-page .layui-laypage span{
                font-size: 0.6rem;
            }
            .layui-table-page .layui-laypage input{
                width: 1.8rem!important;
                padding: 0.1rem;
            }
            .layui-table-cell {
                padding: 2px 4px 2px 6px;
                font-size: 0.6rem;
            }
            .laytable-cell-1-0-0{
                width: 40px!important;
            }
            .laytable-cell-1-0-1{
                width: 70px!important;
            }
            .laytable-cell-1-0-2{
                width: 100px!important;
            }
            .laytable-cell-1-0-3{
                width: 60px!important;
            }
            .laytable-cell-1-0-4{
                width: 80px!important;
            }
            .laytable-cell-1-0-5{
                width: 100px!important;
            }
            .laytable-cell-1-0-6{
                width: 50px!important;
            }
            .laytable-cell-1-0-7{
                width: 90px!important;
            }
            .layui-btn-xs {
                padding: 0 4px;
                font-size: 0.6rem;
            }

        /*开号弹出页*/
            .layui-layer{
                width: 92%!important;
                left: 4%!important;
                height: 100%;
            }
            #builddiv{
                width: 100%!important;
                height: auto!important;
            }
            .layui-layer-title{
                font-size: 0.8rem!important;
            }
            .layui-form-item{
                margin-bottom: 0.4rem!important;
                font-size: 0.6rem!important;
            }
            .layui-form-pane .layui-form-label{
                width: 30%;
                height: 25px;
                line-height: 10px;
                text-align: left;
                padding: 8px 1px 8px 3px!important;
                min-width: 45px;
            }
            .layui-form-pane .layui-input-block{
                margin-left: 32%;
                width: 50%;
            }
            .layui-input, .layui-select, .layui-textarea{
                height: 25px;
                line-height: 10px;
            }
            .layui-btn{
                font-size: 0.7rem!important;
                height: 25px;
                line-height: 25px;
                padding: 0 4px;
            }
            .layui-btn+.layui-btn{
                margin-left: 2%;
            }
        /*修改弹出页*/
            #buildUpdateDiv{
                width: 100%!important;
                height: auto!important;
            }
        /*删除*/
            .layui-layer-dialog{
                width: 90%!important;
                height: 50%;
                min-width: 130px!important;
                min-height: 150px!important;
            }
            .layui-layer-title{
                padding: 0 50% 0 20px;
            }
            .layui-layer-dialog .layui-layer-content{
                font-size: 0.7rem!important;
            }
            .layui-layer-btn{
                padding: 0 0 12px!important;
            }
            .layui-layer-btn .layui-layer-btn0{
                font-size: 0.6rem!important;
                height: 20px;
                line-height: 20px;
                padding: 3px 2px;
                margin: 0;
            }
            .layui-layer-btn .layui-layer-btn1{
                font-size: 0.6rem!important;
                height: 20px;
                line-height: 20px;
                padding: 3px 2px;
                margin: 0 10px;
            }

        }
    </style>
</head>

<body>
<script src="./layui/layui.js"></script>
<div style="display: none;width: 1200px;height: 800px" id="builddiv" >
    <form class="layui-form layui-form-pane" action="/user/add" target="/usershow" id="userform">
    <input  name="idedit" style="display: none" >
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username" lay-verify="username|required"   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">真实姓名</label>
        <div class="layui-input-block">
            <input type="text" name="realName" lay-verify="realName|required"   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="text" name="password" lay-verify="password|required"   class="layui-input">
        </div>
    </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门</label>
            <div class="layui-input-block">
                <select id="department" name="departmentId"lay-verify="required" >
                    <option value=""></option>
                </select>
            </div>
        </div>
    <div class="layui-form-item">
        <label class="layui-form-label">职位</label>
        <div class="layui-input-block">
            <select id="position" name="positionId"lay-verify="required" >
                <option value=""></option>
            </select>
        </div>
    </div>
        <div class="layui-form-item">
            <label class="layui-form-label">员工编号</label>
            <div class="layui-input-block">
                <input type="text" name="employeeNumber" lay-verify="employeeNumber|required"   class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit=""lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<div style="display: none;width: 1200px;height: 800px" id="buildUpdateDiv" >
    <form class="layui-form layui-form-pane" action="/user/updateUserExceptPassword" target="/usershow" id="userform1" lay-filter="formUpdate">
        <input  name="userId" style="display: none" >
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" lay-verify="username|required"   class="layui-input" disabled="disabled">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">真实姓名</label>
            <div class="layui-input-block">
                <input type="text" name="realName" lay-verify="realName|required"   class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="text" name="password"   class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门</label>
            <div class="layui-input-block">
                <select id="department1" name="departmentId"lay-verify="required" >
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">职位</label>
            <div class="layui-input-block">
                <select id="position1" name="positionId"lay-verify="required" >
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">员工编号</label>
            <div class="layui-input-block">
                <input type="text" name="employeeNumber" lay-verify="employeeNumber|required"   class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">在职状态</label>
            <div class="layui-input-block">
                <select id="isIncumbency" name="isIncumbency"lay-verify="required" >
                    <option value="0">离职</option>
                    <option value="1" selected>在职</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit=""lay-filter="demo2">提交修改</button>
<!--                <button type="reset" class="layui-btn layui-btn-primary">重置</button>-->
            </div>
        </div>
    </form>
</div>
<table class="layui-hide" id="usertable" lay-filter="usertable"></table>
<script type="text/html" id="toolbarDemo" >
    <div class="layui-inline">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-inline">
            <input type="text" name="username" id="username" autocomplete="off" class="layui-input">
        </div>
    </div>
    <button class="layui-btn" lay-event="query">查找</button>
    <button class="layui-btn" lay-event="build">开号</button>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="updateUser" style="background: #67b168">修改</a>
</script>
<script th:inline=none>
    layui.use(['table', 'dropdown','form'], function () {
        var table = layui.table;
        var $ = layui.$;
        var form=layui.form
        $.ajax({
                url: '/position/queryposition1',
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    $.each(data.data, function (index, item) {
                        $('#position').append(new Option(item.positionName, item.positionId));// 下拉菜单里添加元素
                        $('#position1').append(new Option(item.positionName, item.positionId));// 下拉菜单里添加元素
                    });
                    layui.form.render("select");
                }, error: function () {
                    alert("仓库列表加载失败")
                }
            }
        )
        $.ajax({
                url: '/department/querydepartment1',
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    $.each(data.data, function (index, item) {
                        $('#department').append(new Option(item.departmentName, item.departmentId));// 下拉菜单里添加元素
                        $('#department1').append(new Option(item.departmentName, item.departmentId));// 下拉菜单里添加元素
                    });
                    layui.form.render("select");
                }, error: function () {
                    alert("仓库列表加载失败")
                }
            }
        )
        layui.form.verify({
            //表单验证
            username: function (value, item) {
                if (!/^[a-zA-Z0-9_]+$/.test(value)){
                    return '请输入数字字母和下划线';
                }
                if (value.length>=30){
                    return '请输入小于三十个字符';
                }
            },
            realName: function (value, item) {
                if (value.length>=30){
                    return '请输入小于三十个字符';
                }

                if (!/[\u4e00-\u9fa5]/.test(value)) {
                    return '请输入真实姓名';
                }
            },
            password: function (value, item) {
                var regex = /^[0-9a-zA-Z_]+$/;
                if (value.length>=24){
                    return '请输入小于24个字符';
                }
                if (!regex.test(value)) {
                    return '请输入数字字母下划线';
                }
            },
            employeeNumber: function (value, item) {
                var regex = /^[0-9a-zA-Z_]+$/;
                if (value.length>=24){
                    return '请输入小于24个字符';
                }
                if (!regex.test(value)) {
                    return '请输入数字字母';
                }
            },

        })
        layui.form.on('submit(demo1)', function(data){
            //demo1表单提交（开户）
            $.post("/user/add",{
                "username" : data.field.username,
                "realName" : data.field.realName,
                "password" : data.field.password,
                "positionId" : data.field.positionId,
                "departmentId":data.field.departmentId,
                "employeeNumber":data.field.employeeNumber,
            }, function(data) {
                if(data.code==20000){
                    layer.msg(data.msg)
                    table.reload('table1');
                }
                else {
                    $('input[name="username"]').val('');
                    $('input[name="realName"]').val('');
                    $('input[name="password"]').val('');
                    $('select[name="departmentId"]').val('');
                    $('select[name="positionId"]').val('');
                    $('input[name="employeeNumber"]').val('');
                    layer.closeAll()
                    layer.msg("新建成功")
                    table.reload('table1');
                    form.render();
                }

            });
            return false;
        });
        layui.form.on('submit(demo2)', function(data){
            //demo2表单提交（修改）
            $.post("/user/updateUserExceptPassword",{
                "userId":data.field.userId,
                "username" : data.field.username,
                "realName" : data.field.realName,
                "password":data.field.password,
                "positionId" : data.field.positionId,
                "departmentId":data.field.departmentId,
                "employeeNumber":data.field.employeeNumber,
                "isIncumbency":data.field.isIncumbency
            }, function(data) {
                if(data.code==20000){
                    layer.msg(data.msg)
                    table.reload('table1');
                }
                else {
                    $('input[name="username"]').val('');
                    $('input[name="realName"]').val('');
                    $('input[name="password"]').val('');
                    $('select[name="departmentId"]').val('');
                    $('select[name="positionId"]').val('');
                    $('input[name="employeeNumber"]').val('');
                    $('input[name="isIncumbency"]').val('');
                    layer.closeAll()
                    layer.msg("修改成功")
                    table.reload('table1');
                    form.render();
                }

            });
            return false;
        });
        table.render({
            //表格渲染
            elem: '#usertable',
            id: 'table1',
            url: '/user/query'
            , response: {
                statusCode: 10000 //规定成功的状态码，默认：0
            }
            , toolbar: '#toolbarDemo'
            , height: 'full-200'
            , cellMinWidth: 80,
              limit:20,
              limits:[20]
            , page: true
            , cols: [[
                {field: 'userId', fixed: 'left', width: 80, title: 'userId'}
                , {field: 'username', width: 120, title: '用户'}
                , {field: 'positionName', title: '职位'}
                , {field: 'departmentName', title: '所属部门'}
                , {field: 'realName', title: '真实姓名',templet:function (d){
                        if (d.isIncumbency === 0){
                            return '<span style="color: #d58512">' + d.realName + '</span>';
                        }
                        if (d.isIncumbency === 1){
                            return '<span>' + d.realName + '</span>'
                        }
                    }}
                , {field: 'employeeNumber', title: '员工编号'}
                ,{field: 'isIncumbency',title:'在职状态',templet: function(d){
                    if (d.isIncumbency === 0){
                        return '<span style="color: red">离职</span>';
                    }
                    if (d.isIncumbency === 1){
                        return '<span style="color: #67b168">在职</span>'
                    }
                    }}
                , {title: '操作', toolbar: '#barDemo'}
            ]]
            , error: function (res, msg) {
                console.log(res, msg)
            }
            , parseData: function (res) {
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.list //解析数据列表
                };
            },

        });
        table.on('toolbar(usertable)', function (obj) {
            switch (obj.event) {
                case 'build':
                    layer.open({
                        title: '开号',
                        type: 1,
                        shadeClose: true,
                        content: layui.$('#builddiv')
                    });
                    break;
                case 'query':
                    table.reload('table1', {
                        where: {
                            username: $('#username').val(),
                        }
                    });
            }

        });
        table.on('tool(usertable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('删除选项', {
                    btn: ['确认删除', '还是算了'] //按钮
                }, function () {
                    $.ajax({
                            url: '/user/delete?userId=' + data.userId,
                            dataType: 'json',
                            type: 'post',
                            success: function (data) {
                                layer.closeAll();
                                table.reload('table1');
                            }, error: function () {
                                alert("删除失败")
                                layer.closeAll();
                            }
                        }
                    )
                }, function () {
                    layer.closeAll();
                });

            }else if (obj.event === 'updateUser'){
                //将点击行的数据显示到修改表单
                form.val("formUpdate",{
                    "userId":obj.data.userId,
                    "username":obj.data.username,
                    "realName":obj.data.realName,
                    "password":obj.data.password,
                    "employeeNumber":obj.data.employeeNumber,
                    "isIncumbency":obj.data.isIncumbency
                });
                layui.use(['form'],function (){
                    var form = layui.form;
                    var departmentId = obj.data.departmentId;
                    var positionId = obj.data.positionId;
                    $("#department1").val(departmentId);
                    $("#position1").val(positionId);
                    form.render();
                });
                //弹出修改页面
                layer.open({
                    title: '用户修改',
                    type: 1,
                    shadeClose: true,
                    content: layui.$('#buildUpdateDiv')
                });
            }
        })


    })
</script>

</body>
</html>